package com.itboye.mengwu.app.ui.aiwujia;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.app.service.util.DataLoaderthree;
import com.itboye.app.service.util.ItemAdapterOne;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.BasePtr;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.address.AddressSelectView2;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.MaMaZengFaBuFenLeiBean;
import com.itboye.mengwu.data.model.WodeZengSongShangPingBean;
import com.itboye.mengwu.data.presenter.NewMaMaZengPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class ZengSongListFragment extends BaseFragment implements Observer,
        OnClickListener {

    private GridView zengsong_list_gv;
    private DataLoaderthree loader;
    private int page = 1;
    public final static int PAGE_SIZE = 10; // 每次加载10个item
    private final static int COUNT = 60;// 最多加载215个，确定加载数。
    private ItemAdapterOne adapter;
    private List<String> list = new ArrayList<String>();
    private boolean isLoadFinished;
    private HashMap<String, String> loaderMap = new HashMap<String, String>();
    RelativeLayout quyuLayout, fenleiLayout, paixuLayout;
    FrameLayout jumpWindow;

    private PopupWindow popupWindow;

    private NewMaMaZengPresenter newMaMaZengPresenter;
    WodeZengSongShangPingBean wodeZengSongShangPingBean,
            wodeZengSongShangPingBeanTwo;
    LuntanTieZiBean luntanTieZiBean;
    AddressSelectView2 addressselect;
    int moveTo;

    LinearLayout morenLine, zuixinLine, zuidiLine, zuigaoLine;
    ImageView morenIv, zuixinIv, zuidiIv, zuigaoIv;
    int paixuFlag = 0;

    ArrayList<MaMaZengFaBuFenLeiBean> maMaZengFaBuFenLeiBean;
    MyDaAdapter myDaAdapter;
    MyZhongAdapter myZhongAdapter;
    MyXiaoAdapter myXiaoAdapter;
    ListView daLv, zhongLv, xiaoLv;

    String cateId = "";
    int pageNO = 1;
    boolean ShangLaflag = false;

    String QUYU = "", LEIMU = "", PAIXU = "19";

    TextView quyuTv, fenleiTv, paixuTv;

    TextView nodata;
    PtrFrameLayout ptr;

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = super.handlerError(data);
        ptr.refreshComplete();
        if (entity != null) {
            if (entity.getEventType() == NewMaMaZengPresenter.LiebiaofenyeSuccess) {
                wodeZengSongShangPingBean = (WodeZengSongShangPingBean) entity
                        .getData();
                if (ShangLaflag == true) {
                    wodeZengSongShangPingBeanTwo.getList().addAll(
                            wodeZengSongShangPingBean.getList());
                    adapter.upDate(wodeZengSongShangPingBeanTwo,
                            wodeZengSongShangPingBeanTwo.getList().size());

                    adapter.notifyDataSetChanged();

                } else if (ShangLaflag == false) {
                    initGridView(wodeZengSongShangPingBean);
                    wodeZengSongShangPingBeanTwo = wodeZengSongShangPingBean;
                }
                if (wodeZengSongShangPingBeanTwo == null) {
                    nodata.setVisibility(View.VISIBLE);
                } else if (wodeZengSongShangPingBeanTwo.getList().size() <= 0) {
                    nodata.setVisibility(View.VISIBLE);
                } else {
                    nodata.setVisibility(View.GONE);
                }
            } else if (entity.getEventType() == NewMaMaZengPresenter.LiebiaofenyeFail) {
                nodata.setVisibility(View.VISIBLE);
            }
            if (entity.getEventType() == NewMaMaZengPresenter.LeimubufenyeSuccess) {
                maMaZengFaBuFenLeiBean = (ArrayList<MaMaZengFaBuFenLeiBean>) entity
                        .getData();
            } else if (entity.getEventType() == NewMaMaZengPresenter.LeimubufenyeFail) {

            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    public int initView() {
        // TODO Auto-generated method stub
        return R.layout.activity_zengwuxian_list;
    }

    @Override
    public void onMyActivityCreated() {
        showProgressDialog("加载中，请稍后", true);
        // SPUtils.put(getActivity(), null, SpPublicParams.gridlength,"0");
        // tieZiBaoHanReply = new TieZiBaoHanReply();
        wodeZengSongShangPingBean = new WodeZengSongShangPingBean();
        newMaMaZengPresenter = new NewMaMaZengPresenter(this);
        // newMaMaZengPresenter.libiaofenye(name, cate_id, page_no, page_size,
        // order, zone, uid, to_uid);
        newMaMaZengPresenter.libiaofenye("", LEIMU, pageNO + "", "10", PAIXU,
                QUYU, "", "");
        BasePtr.setPagedPtrStyle(ptr);
        ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageNO++;
                ShangLaflag=true;
                newMaMaZengPresenter.libiaofenye("", LEIMU, pageNO + "", "10", PAIXU,
                        QUYU, "", "");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                pageNO = 1;
                ShangLaflag=false;
                newMaMaZengPresenter.libiaofenye("", LEIMU, pageNO + "", "10", PAIXU,
                        QUYU, "", "");
            }
        });
        newMaMaZengPresenter = new NewMaMaZengPresenter(this);
        newMaMaZengPresenter.leimubufenye();
        pageNO = 1;

        quyuTv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showPopupQuYuWindow(v);

            }
        });
        fenleiTv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showPopupFenLeiWindow(v);
            }

        });
        paixuTv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showPopupPaiXuWindow(v);
            }

        });
        pageNO = 1;
        ShangLaflag = false;

    }

    private void showPopupQuYuWindow(View view) {

        View contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.fragment_quyu, null);
        addressselect = (AddressSelectView2) contentView
                .findViewById(R.id.addressselect);

        contentView.findViewById(R.id.btn_confirm).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        showProgressDialog("加载中，请稍后", true);
                        if (TextUtils.isEmpty(addressselect.getCityName())) {
                            QUYU = addressselect.getProvinceNo() + "";
                        } else {
                            QUYU = addressselect.getCityNo() + "";
                        }
                        // ByAlert.alert(QUYU);
                        ShangLaflag = false;
                        // newMaMaZengPresenter.libiaofenye(name, cate_id,
                        // page_no,
                        // page_size, order, zone, uid, to_uid);
                        newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10",
                                PAIXU, QUYU, "", "");
                        popupWindow.dismiss();
                        quyuTv.setText(addressselect.getProvinceName()
                                + addressselect.getCityName());
                    }
                });

        popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);

        // 一个自定义的布局，作为显示的内容
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(view);

        // popupWindow.setBackgroundDrawable(new BitmapDrawable());

    }

    private void showPopupFenLeiWindow(View v) {
        View contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.activity_mamazengfabu_fenlei, null);
        popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
                800);

        daLv = (ListView) contentView.findViewById(R.id.daLv);
        zhongLv = (ListView) contentView.findViewById(R.id.zhongLv);
        xiaoLv = (ListView) contentView.findViewById(R.id.xiaoLv);

        myDaAdapter = new MyDaAdapter(maMaZengFaBuFenLeiBean, getActivity());
        daLv.setAdapter(myDaAdapter);
        zhongLv.setVisibility(View.GONE);
        xiaoLv.setVisibility(View.GONE);

        contentView.findViewById(R.id.all).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        showProgressDialog("加载中，请稍后", true);
                        // ByAlert.alert("全部分类");
                        LEIMU = "";
                        ShangLaflag = false;
                        newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10",
                                PAIXU, QUYU, "", "");
                        popupWindow.dismiss();
                        fenleiTv.setText("全部分类");
                    }
                });

        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(v);

    }

    private void showPopupPaiXuWindow(View v) {
        // 一个自定义的布局，作为显示的内容

        View contentView = LayoutInflater.from(getContext()).inflate(
                R.layout.fragment_paixu, null);
        morenLine = (LinearLayout) contentView.findViewById(R.id.morenLine);
        morenIv = (ImageView) contentView.findViewById(R.id.morenIv);
        zuixinLine = (LinearLayout) contentView.findViewById(R.id.zuixinLine);
        zuixinIv = (ImageView) contentView.findViewById(R.id.zuixinIv);
        zuidiLine = (LinearLayout) contentView.findViewById(R.id.zuidiLine);
        zuidiIv = (ImageView) contentView.findViewById(R.id.zuidiIv);

        if (paixuFlag == 0) {
            morenIv.setVisibility(View.VISIBLE);
            zuixinIv.setVisibility(View.GONE);
            zuidiIv.setVisibility(View.GONE);

        } else if (paixuFlag == 1) {
            morenIv.setVisibility(View.GONE);
            zuixinIv.setVisibility(View.VISIBLE);
            zuidiIv.setVisibility(View.GONE);

        } else if (paixuFlag == 2) {
            morenIv.setVisibility(View.GONE);
            zuixinIv.setVisibility(View.GONE);
            zuidiIv.setVisibility(View.VISIBLE);

        }

        morenLine.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                morenIv.setVisibility(View.VISIBLE);
                zuixinIv.setVisibility(View.GONE);
                zuidiIv.setVisibility(View.GONE);
                showProgressDialog("加载中，请稍后", true);
                paixuFlag = 0;
                // newMaMaZengPresenter.libiaofenye(name, cate_id, page_no,
                // page_size, order, zone, uid, to_uid);
                PAIXU = "19";

                // newMaMaZengPresenter.libiaofenye(name, cate_id, page_no,
                // page_size, order, zone, uid, to_uid);
                ShangLaflag = false;
                newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10", PAIXU,
                        QUYU, "", "");
                paixuTv.setText("默认排序");
                popupWindow.dismiss();

            }

        });
        zuixinLine.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                morenIv.setVisibility(View.GONE);
                zuixinIv.setVisibility(View.VISIBLE);
                zuidiIv.setVisibility(View.GONE);

                showProgressDialog("加载中，请稍后", true);
                paixuFlag = 1;
                // newMaMaZengPresenter.libiaofenye(name, cate_id, page_no,
                // page_size, order, zone, uid, to_uid);
                PAIXU = "19";

                // newMaMaZengPresenter.libiaofenye(name, cate_id, page_no,
                // page_size, order, zone, uid, to_uid);
                ShangLaflag = false;
                newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10", PAIXU,
                        QUYU, "", "");

                popupWindow.dismiss();
                paixuTv.setText("最新排序");

            }
        });
        zuidiLine.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                morenIv.setVisibility(View.GONE);
                zuixinIv.setVisibility(View.GONE);
                zuidiIv.setVisibility(View.VISIBLE);

                showProgressDialog("加载中，请稍后", true);
                paixuFlag = 2;
                // newMaMaZengPresenter.libiaofenye(name, cate_id, page_no,
                // page_size, order, zone, uid, to_uid);
                PAIXU = "18";
                ShangLaflag = false;

                newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10", PAIXU,
                        QUYU, "", "");

                popupWindow.dismiss();
                paixuTv.setText("人气商品");

            }
        });

        popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(v);

    }
    /**
     * 实例化控件及数据
     */
    private void initGridView(
            WodeZengSongShangPingBean wodeZengSongShangPingBean) {

        adapter = new ItemAdapterOne(getActivity(), wodeZengSongShangPingBean,
                wodeZengSongShangPingBean.getList().size(), this);

        adapter.notifyDataSetChanged();
        zengsong_list_gv.setAdapter(adapter);


        zengsong_list_gv.smoothScrollToPosition(moveTo);
    }

    @Override
    public void onClick(View v) {

    }

    // 最左边的listview的adapter
    public class MyDaAdapter extends BaseAdapter {
        ArrayList<MaMaZengFaBuFenLeiBean> maMaZengFaBuFenLeiBean;
        Context context;

        public MyDaAdapter() {
            super();
        }

        public MyDaAdapter(
                ArrayList<MaMaZengFaBuFenLeiBean> maMaZengFaBuFenLeiBean,
                Context context) {
            this.maMaZengFaBuFenLeiBean = maMaZengFaBuFenLeiBean;
            this.context = context;
        }

        @Override
        public int getCount() {
            return maMaZengFaBuFenLeiBean != null ? maMaZengFaBuFenLeiBean
                    .size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return maMaZengFaBuFenLeiBean != null ? maMaZengFaBuFenLeiBean
                    .get(position) : null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.item_mamazeng_fabu_fenlei, null);
                holder.tv = (TextView) convertView.findViewById(R.id.tv);
                holder.line = (LinearLayout) convertView
                        .findViewById(R.id.line);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(maMaZengFaBuFenLeiBean.get(position).getName());

            holder.line.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (maMaZengFaBuFenLeiBean.get(position) != null
                            && maMaZengFaBuFenLeiBean.get(position).getChild()
                            .size() > 0) {
                        myZhongAdapter = new MyZhongAdapter(
                                maMaZengFaBuFenLeiBean, context, position);
                        zhongLv.setVisibility(View.VISIBLE);
                        if (myZhongAdapter != null) {
                            zhongLv.setAdapter(myZhongAdapter);
                        }
                    } else {
                        LEIMU = maMaZengFaBuFenLeiBean.get(position).getId();
                        // ByAlert.alert(maMaZengFaBuFenLeiBean.get(position)
                        // .getName());
                        // newMaMaZengPresenter.libiaofenye("", "", "1", "10",
                        // "18", "", "", "");
                        showProgressDialog("加载中，请稍后", true);
                        ShangLaflag = false;
                        newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10",
                                PAIXU, QUYU, "", "");
                        popupWindow.dismiss();
                        fenleiTv.setText(maMaZengFaBuFenLeiBean.get(position)
                                .getName());

                    }

                }
            });
            return convertView;

        }
    }

    // 中间的listview的adpter
    class MyZhongAdapter extends BaseAdapter {
        ArrayList<MaMaZengFaBuFenLeiBean> entity;
        Context context;
        int daPosition;

        public MyZhongAdapter() {
            super();
        }

        public MyZhongAdapter(ArrayList<MaMaZengFaBuFenLeiBean> entity,
                              Context context, int daPosition) {
            super();
            this.entity = entity;
            this.context = context;
            this.daPosition = daPosition;
        }

        @Override
        public int getCount() {
            return entity != null ? entity.get(daPosition).getChild().size()
                    : 0;
        }

        @Override
        public Object getItem(int position) {
            return entity != null ? entity.get(daPosition).getChild()
                    .get(position) : null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.item_mamazeng_fabu_fenlei, null);
                holder.tv = (TextView) convertView.findViewById(R.id.tv);
                holder.line = (LinearLayout) convertView
                        .findViewById(R.id.line);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(entity.get(daPosition).getChild().get(position)
                    .getName());
            holder.line.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (entity.get(daPosition).getChild().get(position)
                            .getChild() != null
                            && entity.get(daPosition).getChild().get(position)
                            .getChild().size() > 0) {
                        myXiaoAdapter = new MyXiaoAdapter(entity
                                .get(daPosition).getChild().get(position),
                                context, daPosition, position);
                        xiaoLv.setAdapter(myXiaoAdapter);
                        xiaoLv.setVisibility(View.VISIBLE);

                    } else {
                        showProgressDialog("加载中，请稍后", true);
                        ShangLaflag = false;
                        // ByAlert.alert(maMaZengFaBuFenLeiBean.get(daPosition)
                        // .getName()
                        // + maMaZengFaBuFenLeiBean.get(daPosition)
                        // .getChild().get(position).getName());
                        LEIMU = maMaZengFaBuFenLeiBean.get(daPosition)
                                .getChild().get(position).getId();
                        newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10",
                                PAIXU, QUYU, "", "");
                        popupWindow.dismiss();
                        fenleiTv.setText(maMaZengFaBuFenLeiBean.get(daPosition)
                                .getChild().get(position).getName());
                    }

                }
            });
            return convertView;
        }

    }

    // 最右边的listView的Adpter
    class MyXiaoAdapter extends BaseAdapter {
        MaMaZengFaBuFenLeiBean.B entity;
        private LayoutInflater inflater;
        Context context;
        int bigPosition, zhongPosition;

        public MyXiaoAdapter() {
            super();
        }

        public MyXiaoAdapter(MaMaZengFaBuFenLeiBean.B entity, Context context,
                             int bigPosition, int zhongPosition) {
            super();
            this.entity = entity;
            this.context = context;
            this.bigPosition = bigPosition;
            this.zhongPosition = zhongPosition;
        }

        @Override
        public int getCount() {
            return entity.getChild() != null ? entity.getChild().size() : 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return entity.getChild() != null ? entity.getChild().get(position)
                    : null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.item_mamazeng_fabu_fenlei, null);
                holder.tv = (TextView) convertView.findViewById(R.id.tv);
                holder.line = (LinearLayout) convertView
                        .findViewById(R.id.line);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(entity.getChild().get(position).getName());
            holder.line.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    showProgressDialog("加载中，请稍后", true);
                    ShangLaflag = false;
                    // ByAlert.alert(maMaZengFaBuFenLeiBean.get(bigPosition)
                    // .getName()
                    // + maMaZengFaBuFenLeiBean.get(bigPosition)
                    // .getChild().get(zhongPosition).getName()
                    // + maMaZengFaBuFenLeiBean.get(bigPosition)
                    // .getChild().get(zhongPosition).getChild()
                    // .get(position).getName());
                    LEIMU = maMaZengFaBuFenLeiBean.get(bigPosition).getChild()
                            .get(zhongPosition).getChild().get(position)
                            .getId();
                    newMaMaZengPresenter.libiaofenye("", LEIMU, "1", "10",
                            PAIXU, QUYU, "", "");
                    popupWindow.dismiss();
                    fenleiTv.setText(maMaZengFaBuFenLeiBean.get(bigPosition)
                            .getChild().get(zhongPosition).getChild()
                            .get(position).getName());
                }
            });

            return convertView;
        }

    }

    class ViewHolder {

        public TextView tv;
        public LinearLayout line;
    }

}
